package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.ChainSkuDAO;
import com.junzhiit.nengji.model.ChainSku;
import com.junzhiit.nengji.model.Sku;

@Repository
public class ChainSkuDAOImpl extends AbstractBaseDAO<ChainSku> implements ChainSkuDAO{
	
	@Override
	public List<Sku> findAll(ChainSku chainSku, int pageNumber, int maxPageRow) {

		StringBuffer sql = new StringBuffer();
		sql.append(" select * from chain_sku where is_delete=? and chain_id=? ");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainSku.getChainId());

		if (StringUtils.isNotBlank(chainSku.getName())) {
			sql.append(" and name like ? ");
			param.setString("%" + chainSku.getName() + "%");
		}
		sql.append(" order by created_time desc ");

		return queryForPagingList(sql.toString(), Sku.class, param, pageNumber, maxPageRow);

	}
	
	
	@Override
	public int getTotalCount(ChainSku chainSku) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from chain_sku where is_delete=? and chain_id=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainSku.getChainId());

		if (StringUtils.isNotEmpty(chainSku.getName())) {
			sql.append(" and name like ? ");
			param.setString("%" + chainSku.getName() + "%");
		}

		return queryForInt(sql.toString(), param);
	}
	
	@Override
	public List<ChainSku> queryAllSku(String chainId){
		StringBuffer sql=new StringBuffer();
		sql.append(" select id,name from chain_sku");
		sql.append(" where is_delete=? and chain_id=?");
		sql.append(" order by created_time desc");
		
		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainId);
		
		return queryForList(sql.toString(), ChainSku.class, param);
	}

	@Override
	public int queryNameIsExit(String chainId, String name) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(id) from chain_sku");
		sql.append(" where is_delete=? and chain_id=? and name=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainId);
		param.setString(name);

		return queryForInt(sql.toString(), param);
	}
	
}
